package net.nshc.kwkim.mycalc.activity;

import java.io.*;
import java.util.*;

import net.nshc.kwkim.mycalc.*;
import android.content.*;
import android.location.*;
import android.os.*;
import android.util.*;
import android.widget.*;

import com.google.android.maps.*;


public class MapDialog2 extends MapActivity {
	
	private LocationManager mLocMan;
	
	private TextView mStatus, mAddress;
	
	private Geocoder mCoder;
	
	private String mProvider;
	
	private List<Address> addr;
	
	private MapView mMap;
	
	private MapController mControl;
	
	private MyLocation mPoint;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		mMap = (MapView)findViewById(R.id.mapview);
		mLocMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		mStatus = (TextView)findViewById(R.id.status);
		mAddress = (TextView)findViewById(R.id.address);
		mProvider = mLocMan.getBestProvider(new Criteria(), true);
		mCoder = new Geocoder(this);
		mControl = mMap.getController();
		mControl.setZoom(16);
		mMap.setBuiltInZoomControls(true);
		mPoint = new MyLocation(this, mMap);
	}
	
	public void onResume() {
		
		super.onResume();
		mPoint.enableMyLocation();
		mPoint.enableCompass();
		mLocMan.requestLocationUpdates(mProvider, 3000, 10, mListener);
		mStatus.setText("현재 상태 : 서비스 시작");
		
	}
	
	public void onPause() {
		
		super.onPause();
		mPoint.disableMyLocation();
		mPoint.disableCompass();
		mLocMan.removeUpdates(mListener);
		mStatus.setText("현재 상태 : 서비스 정지");
		
	}

	LocationListener mListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			Double Latitude, Longitude;
			Latitude = location.getLatitude();
			Longitude = location.getLongitude();
			addr = null;

			try{
				addr = mCoder.getFromLocation(Latitude, Longitude, 5);
			} catch (IOException e){
				mAddress.setText("IO error : "+e.getMessage());
				return;
			}
			
			if(addr == null){
				mAddress.setText("no result");
				return;
			}
			
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_MAPACTIVITY, "LOCATION INFORMATION_Before Latitude = "+Latitude+", Longitude = "+Longitude);
			
			Latitude = (Latitude*1E6);
			Longitude = (Longitude*1E6);
			GeoPoint pt = new GeoPoint(Latitude.intValue(), Longitude.intValue());
			
			if(MyCalcActivity.bLog)Log.d(LogDefine.LOG_MAPACTIVITY, "LOCATION INFORMATION_After Latitude = "+Latitude+", Longitude = "+Longitude);
			
			mControl.setCenter(pt);
			List<Overlay> overlays = mMap.getOverlays();
			overlays.add(mPoint);
			
			mPoint.runOnFirstFix(new Runnable() {
				
				public void run() {
					mMap.getController().animateTo(mPoint.getMyLocation());
				}
			});
			
			String temp = addr.get(0).toString();
			String[] arTemp = temp.split("\""); 
			mAddress.setText("주소 : "+arTemp[1]);
		}

		public void onProviderDisabled(String provider) {
			mStatus.setText("현재 상태 : 서비스 사용 불가");
		}

		public void onProviderEnabled(String provider) {
			mStatus.setText("현재 상태 : 서비스 사용 가능");
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			String sStatus = "";
			switch (status) {
			case LocationProvider.OUT_OF_SERVICE:
				sStatus = "범위 벗어남";
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				sStatus = "일시적 불능";
				break;
			case LocationProvider.AVAILABLE:
				sStatus = "사용 가능";
				break;
			}
			mStatus.setText(provider + "상태 변경 : " + sStatus);
		}
	};
	
	public void setMap(){
		
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	
}